
package System;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedList;

/**
 * @author Shinichi
 * Implements listener as thread manager
 * each time a new source is added it creates a watcher for this source
 * and runs it in new thread named as this source this way we can easily find it later
 * implements the methods of listener IF
 */
class PooledListener implements Listener{
	private Boolean stop=false;
	private LinkedList<WatchDir> pool=new LinkedList<WatchDir>();
    private boolean closed = true;
    private int poolSize = 100;
    public LinkedList<Classifier> myClassifiers=new LinkedList<Classifier>();
	
    public PooledListener(Classifier clasi){
    	myClassifiers.add(clasi);
    }
    
    /**
     * adding a new source to listen to
     */
    public void execute(String path, String[] exp) {
		try {
			Path dir=Paths.get(path);
			WatchDir newbie=new WatchDir(exp,myClassifiers, dir, true);
			newbie.setName(path);
			pool.add(newbie);
			pool.getLast().start();
		} catch (IOException e) {}
	}
    /**
     * removal of one thread from list
     */
    public void undo(String name){
    	//name=name.replace('_','\\');
    	for (int i=0;i<pool.size();i++) {
    		System.out.println("name:"+name);
    		System.out.println("pool:"+pool.get(i).getName());
    		if (pool.get(i).getName().equals(name)) {
				try {
					pool.remove(i).interrupt();
				} catch (Exception e) {}
    			
			}
			
		}
    }
	public LinkedList<Classifier> getClassifiers() {
		return myClassifiers;
	}

	@SuppressWarnings("deprecation")
	public void listenningOff() {
		for (Thread th : pool) {
			th.suspend();
		}
		
	}

	@SuppressWarnings("deprecation")
	public void listenningOn() {
		for (Thread th : pool) {
			th.resume();
		}
		
	}

	public void start() {
		for (Thread th : pool) {
			th.start();
		}
		
	}

	public void stop() {
		// TODO Auto-generated method stub
		
	}
	
}

